package cn.itcast.config;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

@Cacheable
@EnableWebSecurity
@Component
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Bean
    public PasswordEncoder password() {
        return new BCryptPasswordEncoder();
    }


    @Override
    public void configure(WebSecurity web) throws Exception {

    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
                http
                .formLogin()
                .loginPage("/login.html") //使用formLogin的方式登陆
                .loginProcessingUrl("/login")//指定自定义登录逻辑，需要表单的action对应
                .successForwardUrl("/tomain")
                .and()
                .authorizeRequests()
                .mvcMatchers("/login.html").permitAll()
                .mvcMatchers("/css/**").permitAll()
                .mvcMatchers("/img/**").permitAll()
                .mvcMatchers("/js/**").permitAll()
                .mvcMatchers("/plugins/**").permitAll()
                .mvcMatchers("/template/**").permitAll()
                .anyRequest().authenticated()
                .and()
                .headers()
                .frameOptions()
                .sameOrigin()
                .and()
                .csrf()
                .disable();
    }

}

